package com.djinfo.work.util.activemq;

import com.djinfo.work.entity.SysMessageEntity;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created by lenovo on 2017/8/21.
 */
public class Sender {
    public static void Send(SysMessageEntity sysMessageEntity){
        // ConnectionFactory ：连接工厂，JMS 用它创建连接
        ConnectionFactory connectionFactory;
        // Provider 的连接
        Connection connection = null;// Connection ：JMS 客户端到JMS
        Session session;  // Session： 一个发送或接收消息的线程
        Destination destination; // Destination ：消息的目的地;消息发送给谁.
        MessageProducer producer; // MessageProducer：消息发送者
        // 构造ConnectionFactory实例对象，此处采用ActiveMq的实现jar
        connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
        try { // 构造从工厂得到连接对象
            connection = connectionFactory.createConnection();
            // 启动
            connection.start();
            // 获取操作连接
            session = connection.createSession(Boolean.TRUE,
                    Session.AUTO_ACKNOWLEDGE);
            // 获取session注意参数值xingbo.xu-queue是一个服务器的queue，须在在ActiveMq的console配置
            destination = session.createQueue("FirstQueue");
            // 得到消息生成者【发送者】
            producer = session.createProducer(destination);
            // 设置不持久化，此处学习，实际根据项目决定
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            // 构造消息，此处写死，项目就是参数，或者方法获取
            sendMessage(session, producer,sysMessageEntity);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != connection)
                    connection.close();
            } catch (Throwable ignore) {
            }
        }
    }

    public static void sendMessage(Session session, MessageProducer producer,SysMessageEntity sysMessageEntity)
            throws Exception {
        TextMessage message = session.createTextMessage("<RequestXml>"+
                "<id>"+sysMessageEntity.getId()+"</id>" +
                "<publish>"+sysMessageEntity.getPublish()+"</publish>" +
                "<title>"+sysMessageEntity.getTitle()+"</title>" +
                "<content>"+sysMessageEntity.getContent()+"</content>" +
                "<receiveObject>"+sysMessageEntity.getReceiveObject()+"</receiveObject>" +
                "<attachMent>"+sysMessageEntity.getAttachMent()+"</attachMent>" +
                "<status>"+sysMessageEntity.getStatus()+"</status>" +
                "<type>"+sysMessageEntity.getType()+"</type>" +
                "<imageShow>"+sysMessageEntity.getImageShow()+"</imageShow>" +
                "<createTime>"+sysMessageEntity.getCreateTime()+"</createTime>" +
                "<updateTime>"+sysMessageEntity.getUpdateTime()+"</updateTime>" +
                "<createUser>"+sysMessageEntity.getCreateUser()+"</createUser>" +
                "<updateUser>"+sysMessageEntity.getUpdateUser()+"</updateUser>" +
                "</RequestXml>");
            // 发送消息到目的地方
            System.out.println("<RequestXml>"+
                    "<id>"+sysMessageEntity.getId()+"</id>" +
                    "<publish>"+sysMessageEntity.getPublish()+"</publish>" +
                    "<title>"+sysMessageEntity.getTitle()+"</title>" +
                    "<content>"+sysMessageEntity.getContent()+"</content>" +
                    "<receiveObject>"+sysMessageEntity.getReceiveObject()+"</receiveObject>" +
                    "<attachMent>"+sysMessageEntity.getAttachMent()+"</attachMent>" +
                    "<status>"+sysMessageEntity.getStatus()+"</status>" +
                    "<type>"+sysMessageEntity.getType()+"</type>" +
                    "<imageShow>"+sysMessageEntity.getImageShow()+"</imageShow>" +
                    "<createTime>"+sysMessageEntity.getCreateTime()+"</createTime>" +
                    "<updateTime>"+sysMessageEntity.getUpdateTime()+"</updateTime>" +
                    "<createUser>"+sysMessageEntity.getCreateUser()+"</createUser>" +
                    "<updateUser>"+sysMessageEntity.getUpdateUser()+"</updateUser>" +
                    "</RequestXml>");
            producer.send(message);

    }
}
